﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using BySoft.Server.Business.UserCode;
using BySoft.Server.Common.Enum;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Server.Common;
using System.Data;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Text;


namespace BySoft.Web.Site.ComPage
{
    public partial class UpFilePage : BasePage
    {
        private DataTable CreateUpFileDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AttachmentID");
            dt.Columns.Add("FileType");
            dt.Columns.Add("FileSize");
            dt.Columns.Add("UpDateTime");
            dt.Columns.Add("FileURL");
            dt.Columns.Add("FileName");
            dt.Columns.Add("FormType");
            dt.Columns.Add("FormID");
            dt.Columns.Add("Status");
            return dt;
        }

        protected DataTable DtUpFile
        {
            get
            {
                if (Session["DtUpFile"] != null)
                {
                    return (DataTable)Session["DtUpFile"];
                }
                else
                {
                    return CreateUpFileDT();
                }
            }
            set
            {
                Session["DtUpFile"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidFileFormType.Value = UIHelper.GetQueryStringValue("id");
            }
        }

        private void SaveFile(string strDir,string strFormType, FileUpload fi)
        {
            string strName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string strServerDir = Server.MapPath(strDir);
            if (!Directory.Exists(strServerDir))
            {
                Directory.CreateDirectory(strServerDir);
            }
            //string strFileName = strServerDir + strName;
            if (fi.HasFile)
            {
                string strUpFileName = fi.FileName.Trim();
                string strExtension = strUpFileName.Substring(strUpFileName.LastIndexOf("."));
                string strSaveFileName = strName + strExtension;
                string strSavePath = strServerDir + strSaveFileName;
                fi.SaveAs(strSavePath);
                FileInfo finfo = new FileInfo(strSavePath);
                DataTable dtTemp = DtUpFile.Copy();
                DataRow drNew = dtTemp.NewRow();
                drNew["AttachmentID"] = Guid.NewGuid().ToString();
                drNew["FileType"] = finfo.Extension;
                drNew["FileSize"] = Convert.ToInt32(finfo.Length);
                drNew["FileURL"] = finfo.Name;
                drNew["FileName"] = strUpFileName;
                drNew["FormType"] = strFormType;
                dtTemp.Rows.Add(drNew);
                DtUpFile = dtTemp.Copy();
            }
            else
            {
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string strPath = "";
            string strFormType = "";
            switch (hidFileFormType.Value.Trim())
            {
                case "1": strFormType = "Quote"; strPath = "~\\UpFile\\请购单\\"; break;
            }

            SaveFile(strPath,strFormType, fimport);
            string sReturn = "";
            sReturn = "success";
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
        }
    }
}